package com.approval.approvalapp.index.dbsx.tshf;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.TshfDbXqBean;
import com.approval.approvalapp.bean.ZxhfxqdbBean;
import com.approval.approvalapp.index.dbsx.tshf.adapter.TxhfxqAdapter;
import com.approval.approvalapp.index.dbsx.tshf.fragment.TsHfDbFragment;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.bumptech.glide.Glide;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BottomPopupView;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

import androidx.annotation.NonNull;
import butterknife.Bind;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

public class TshfxqDbActivity extends BaseActivity2 {
    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.zxr_tv)
    TextView zxrTv;
    @Bind(R.id.zxdw_tv)
    TextView zxdwTv;
    @Bind(R.id.zxsj_tv)
    TextView zxsjTv;
    @Bind(R.id.zxdh_tv)
    TextView zxdhTv;
    @Bind(R.id.contant_tv)
    TextView contantTv;
    @Bind(R.id.button)
    LinearLayout button;
    @Bind(R.id.title_tttv)
    TextView title_tttv;
    @Bind(R.id.rv)
    RecyclerView rv;
    @Bind(R.id.btn)
    Button btn;
    private String title;
    private String zxr;
    private String zxdw;
    private String zxsj;
    private String applyid;
    private TshfDbXqBean tshfdbxq;
    public static TshfxqDbActivity instance;

    @Override
    public int getLayoutId() {

        instance = this;
        return R.layout.activity_tshfxq_db;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        instance = null;
    }

    @Override
    public void initView() {
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = this.getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            this.getWindow().setStatusBarColor(Color.TRANSPARENT);
        }

        title = getIntent().getStringExtra("title");
        zxr = getIntent().getStringExtra("zxr");
        zxdw = getIntent().getStringExtra("zxdw");
        zxsj = getIntent().getStringExtra("zxsj");
        applyid = getIntent().getStringExtra("applyid");

        title_tttv.setText(title);
        zxrTv.setText(zxr);
        zxdwTv.setText(zxdw);
        zxsjTv.setText(zxsj);
    }

    @Override
    public void initData() {
        refreshData();
    }

    private void refreshData() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username", SpUtil.getString(this, SpUtil.USER_NAME, ""));
        jsonObject.put("applyid", applyid);
        NetRequsetUtil.getInstance().netRequestPostJson("v1/user/get_ts_show", jsonObject.toString(), new NetDownResponse() {
            @Override
            public void success(String str) {
                tshfdbxq = JSON.parseObject(str, TshfDbXqBean.class);
                if (zxdhTv == null){
                    return;
                }
                zxdhTv.setText(tshfdbxq.getMphone());
                contantTv.setText(tshfdbxq.getContent());
                rv.setLayoutManager(new LinearLayoutManager(TshfxqDbActivity.this));
                TxhfxqAdapter txhfxqAdapter  = new TxhfxqAdapter();
                txhfxqAdapter.setHandleoptionsBeanList(tshfdbxq.getHandleoptions());
                rv.setAdapter(txhfxqAdapter);
            }

            @Override
            public void errowithresponse(String str) {
                ErrorBeanUtil.getErrorMessage(str);
            }

            @Override
            public void erro() {
                ErrorBeanUtil.getErrorMessage(null);
            }

            @Override
            public void finish() {

            }
        });
    }

    @OnClick({R.id.back_iv, R.id.btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.btn:
                Intent intent = new Intent();
                if (tshfdbxq == null){
                    return;
                }
                intent.putExtra("list", (Serializable) tshfdbxq.getApplydetail().getPassuser());
                intent.setClass(this,TshfContantActivity.class);
                intent.putExtra("applyid",tshfdbxq.getApplydetail().getApplyId());
                intent.putExtra("handleId",tshfdbxq.getApplydetail().getHandleId());
                intent.putExtra("passnode",tshfdbxq.getApplydetail().getPassnode());
                intent.putExtra("currentNode",tshfdbxq.getApplydetail().getCurrentNode());

                startActivity(intent);
//                new XPopup.Builder(this)
//                        .autoOpenSoftInput(true)
//                        .asCustom(new CustomEditTextBottomPopup(this))
//                        .show();
                break;
        }
    }

    private class CustomEditTextBottomPopup extends BottomPopupView {
        public CustomEditTextBottomPopup(@NonNull Context context) {
            super(context);
        }

        @Override
        protected int getImplLayoutId() {
            return R.layout.custom_edittext_bottom_popup_ts;
        }

        @Override
        protected void onCreate() {
            super.onCreate();
        }

        @Override
        protected void onShow() {
            super.onShow();
            findViewById(R.id.btn_finish).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });

            findViewById(R.id.hf_tv).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (TextUtils.isEmpty(getComment())){
                        dismiss();
                        Toast.makeText(TshfxqDbActivity.this, "输入内容为空", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    button.setVisibility(GONE);
                    //提交之后刷新页面 隐藏按钮
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("username",SpUtil.getString(TshfxqDbActivity.this,
                            SpUtil.USER_NAME,""));
                    jsonObject.put("applyid",applyid);
//                    jsonObject.put("passuserid",tshfdbxq.getApplydetail().getPassuserid());
                    jsonObject.put("opinion",getComment());
                    jsonObject.put("currentNode",tshfdbxq.getApplydetail().getCurrentNode());
                    jsonObject.put("passnode",tshfdbxq.getApplydetail().getPassnode());
                    jsonObject.put("handleId",tshfdbxq.getApplydetail().getHandleId());
                    NetRequsetUtil.getInstance().netRequestPostJson("v1/user/handle_ts_hf", jsonObject.toString(),
                            new NetDownResponse() {
                                @Override
                                public void success(String str) {
                                    refreshData();
                                    if (TsHfDbFragment.instance != null){
                                        TsHfDbFragment.instance.refreshData();
                                    }
                                }

                                @Override
                                public void errowithresponse(String str) {

                                }

                                @Override
                                public void erro() {

                                }

                                @Override
                                public void finish() {

                                }
                            });
                    dismiss();
                }
            });
        }



        @Override
        protected void onDismiss() {
            super.onDismiss();
            Log.e("tag", "CustomEditTextBottomPopup  onDismiss");
        }

//

        public String getComment(){
            EditText et = findViewById(R.id.et_comment);
            return et.getText().toString();
        }
    }

}
